package com.wuxuehua.myapp.web.rest;

import com.wuxuehua.myapp.domain.CommitTaskBean;
import com.wuxuehua.myapp.domain.User;
import com.wuxuehua.myapp.repository.UserRepository;
import com.wuxuehua.myapp.security.SecurityUtils;
import com.wuxuehua.myapp.service.dto.TaskDto;
import jdk.nashorn.internal.runtime.logging.Logger;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.impl.identity.Authentication;
import org.activiti.engine.runtime.Execution;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.activiti.engine.task.TaskQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * Created by Administrator on 2017/10/21 0021.
 */
@RestController()
@RequestMapping("/api")
public class TestMyListener {


    @Autowired
    RuntimeService runtimeService;


    @Autowired
    TaskService taskService;

    @Autowired
    UserRepository userRepository;

    //http://localhost:8080/api/test?key=pay_test&activityId=waitpay&transactor=16
    @GetMapping("/test")
    public ResponseEntity<Object> testListener(String key,String activityId,String transactor){
        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey(key);
        String processInstanceId = processInstance.getId();
        //流程启动 进入到接受消息任务 等待支付完成进入商家确认
        Execution execution = runtimeService.createExecutionQuery().processInstanceId(processInstanceId).activityId(activityId).singleResult();

        if (execution == null){
            return new ResponseEntity<Object>("流程出现了一点问题", HttpStatus.INTERNAL_SERVER_ERROR);

        }
        String executionId = execution.getId();
        Map<String, Object> map = new HashMap<>();
        map.put("transactor",null);
        if (transactor != null && !transactor.equals("")){
            map.put("transactor",transactor);
        }
        runtimeService.signal(executionId,map);//进入商家确认

        TaskQuery taskQuery = taskService.createTaskQuery();
        taskQuery.taskAssignee(String.valueOf(16));
        List<Task> list = taskQuery.list();
        List<TaskDto> result = new ArrayList();
        for (Task task : list) {
            TaskDto taskDto = new TaskDto();
            taskDto.setId(task.getId());
            taskDto.setName(task.getName());
            result.add(taskDto);
        }
        return new ResponseEntity<Object>(result, HttpStatus.OK);
    }

    /**
     * 办理任务
     * @param commitTaskBean
     */
    @PostMapping("/task/commit")
    public void commitTask(CommitTaskBean commitTaskBean){

        String taskId = commitTaskBean.getTaskId();
        Task task = taskService.createTaskQuery().taskId(taskId).singleResult();
        String processInstanceId = task.getProcessInstanceId();
        String userLogin = SecurityUtils.getCurrentUserLogin();
        Optional<User> user = userRepository.findOneByLogin(userLogin);
        if (user == null){

        }
        Authentication.setAuthenticatedUserId(String.valueOf(user.get().getId()));
        taskService.addComment(taskId,processInstanceId,commitTaskBean.getMessage());
        Map<String, Object> map = new HashMap<>();
        String transactor = commitTaskBean.getTransactor();
        map.put("transactor",null);
        if (transactor != null && !transactor.equals("")){
            map.put("transactor",transactor);
        }
        String result = commitTaskBean.getResult();
        map.put("result",null);
        if (result != null && !result.equals("")){
            map.put("result",result);
        }

        taskService.complete(taskId,map);

    }

    //http://localhost:8080/api/task/group?id=16
    @GetMapping("/task/group")
    public List<TaskDto> gruop(String id){
        TaskQuery taskQuery = taskService.createTaskQuery();
        taskQuery.taskCandidateUser(id);
        List<Task> list = taskQuery.list();
        List<TaskDto> result = new ArrayList();
        for (Task task : list) {
            TaskDto taskDto = new TaskDto(task.getId(),task.getName());
            result.add(taskDto);
        }

        return result;
    }
}
